package com.irdstudio.efp.nls.service.impl.yx;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.esb.common.constant.MsLoanConstant;
import com.irdstudio.efp.esb.service.bo.req.yx.LetterBasicInfo;
import com.irdstudio.efp.esb.service.bo.req.yx.LetterExpanding;
import com.irdstudio.efp.esb.service.bo.req.yx.LetterMessage;
import com.irdstudio.efp.esb.service.bo.req.yx.LetterReq;
import com.irdstudio.efp.esb.service.bo.resp.yx.LetterResp;
import com.irdstudio.efp.esb.service.facade.yx.LetterService;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.impl.SoltServiceImpl;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;

/**
 * 用信通知接口
  * @author heyingfeng
  * @since 2019年3月19日 下午7:38:21
  * @version 1.0
 */
@Service("letterBackService")
public class LetterBackServiceImpl extends SoltServiceImpl {

	private static Logger logger = LoggerFactory.getLogger(LetterBackServiceImpl.class);

	@Autowired
	@Qualifier("letterService")
	private LetterService letterService;// 用信贷款审核通知

	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;// 网贷申请基本信息

	@Override
	protected void doExecute(NlsProcessBizVO nlsProcessBizVo, NlsApplyInfoVO nlsApplyInfoVO) throws Exception {
		String applySeq = nlsProcessBizVo.getApplySeq();// 流水号
		try {
			logger.info("用信审核通知接口，流水号:" + applySeq);
			Objects.requireNonNull(nlsApplyInfoVO);// 网贷申请信息不能为空
			LetterReq letterReq = new LetterReq();
			LetterMessage letterMessage = new LetterMessage();
			LetterBasicInfo letterBasicInfo = new LetterBasicInfo();
			LetterExpanding letterExpanding = new LetterExpanding();
			letterReq.setRqsFlg(nlsApplyInfoVO.getBdReqSn());//请求方需要保证永远唯一 响应方据此判断是否重复请求
			letterReq.setRetCd(MsLoanConstant.RetCodeEnum.SUCCESS.VALUE);
			letterReq.setTxnMd("async");//交易模式
			/** Message	业务数据 start */
			letterMessage.setSessionId(applySeq);// 交易流水号
			letterMessage.setEvTp("apply");//事务类型
			letterMessage.setEvID(nlsApplyInfoVO.getBdTransactionId());//事务ID
			letterMessage.setRqsTmstmp(TimeUtil.getTimeStampByPattern("yyyyMMddHHmmss"));//时间戳
			/** BasicInfo	基本信息 start */
			letterBasicInfo.setIDCertNo(nlsApplyInfoVO.getCertCode());//身份证号
			letterBasicInfo.setBankCardNo(nlsApplyInfoVO.getRecvAcctNo());//银行卡号
			letterBasicInfo.setCustNm(nlsApplyInfoVO.getCusName());//客户姓名
			letterBasicInfo.setMblNo(nlsApplyInfoVO.getMobile());//手机号码
			/** BasicInfo	基本信息 end */
			/** Expanding	定制域 start */
			letterExpanding.setInstRsnCd(nlsApplyInfoVO.getBdReasonCode());	//机构原因码
			letterExpanding.setInstRsnCdRmk(nlsApplyInfoVO.getBdReasonMsg());	//机构原因码说明
			letterExpanding.setInstPolcyCd(MsLoanConstant.RisCodeEnum.SUCCESS.VALUE);	//机构策略码
			/** Expanding	定制域 end */
			letterMessage.setBasicInfo(letterBasicInfo);
			letterMessage.setExpanding(letterExpanding);
			letterReq.setMessage(letterMessage);
			
			boolean isSend = false;// 是否发送通知请求
			nlsApplyInfoVO.setDnSts(MsLoanConstant.DN_STS_3);// 放款状态
			logger.info("用信审核通知更新放款状态信息：" + JSONObject.toJSONString(nlsApplyInfoVO));
			int num = nlsApplyInfoService.updateByPk((nlsApplyInfoVO));
			logger.info("用信审核通知更新放款状态信息：" + JSONObject.toJSONString(num));
			if (num > 0) {
				isSend = true;
			}
			if (isSend) {// 发送请求
				LetterResp letterResp = letterService.letterNotify(letterReq);// 发送请求
				Objects.requireNonNull(letterResp);
				if (!MsLoanConstant.RetCodeEnum.SUCCESS.VALUE.equals(letterResp.getRetCd()) || !"E1001".equals(letterResp.getRetCd())) {// 0：请求成功，其他编码均代表失败
					logger.error("调用用信审批结果消费接口返回信息");
				}
			}
		} catch (Exception e) {
			logger.error("用信贷款审核通知出现异常：" + e.getMessage());
//			nlsProcessBizVo.setExceptionFlag(true);
//			nlsProcessBizVo.setExceptionMsg(e);
		}
	}
}
